import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';

class SwiperUtil extends StatelessWidget {

  final List<String> images;
  final double width;
  final double height;
  final double viewportFraction;
  final double scale; 
  final double borderRadius;
  final bool autoplay;
  final double padding;
  final SwiperPlugin? pagination;

  SwiperUtil({
    required this.images, 
    required this.width, 
    required this.height, 
    this.viewportFraction = 1, 
    this.scale = 1,
    this.borderRadius = 0,
    this.autoplay = true,
    this.padding = 0,
    this.pagination
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      child: images.length <= 0 ? Container() : Swiper(
        itemBuilder: (BuildContext context, int index) {
          return Container(
            padding: EdgeInsets.symmetric(horizontal: padding),
            child: NetworkImageUtil(
              url: images[index], 
              width: width, 
              height: height,
              borderRadius: borderRadius,
            ),
          );
        },
        itemCount: images.length,
        autoplay: autoplay,
        viewportFraction: viewportFraction,
        scale: scale,
        pagination: pagination ?? SwiperCustomPagination(
          builder: (BuildContext context, SwiperPluginConfig config) {
            return Container(
              margin: EdgeInsets.only(bottom: 8.h),
              alignment: Alignment.bottomCenter,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: images.asMap().keys.map((index) {
                  if (index == config.activeIndex) {
                    return Container(
                      width: 18.w,
                      height: 5.w,
                      margin: EdgeInsets.symmetric(horizontal: 3.w),
                      decoration: BoxDecoration(
                        color: bgWhiteColor,
                        borderRadius: BorderRadius.circular(5.w)
                      ),
                    );
                  } else {
                    return Container(
                      width: 5.w,
                      height: 5.w,
                      margin: EdgeInsets.symmetric(horizontal: 3.w),
                      decoration: BoxDecoration(
                        color: bgSubColor,
                        borderRadius: BorderRadius.circular(5.w)
                      ),
                    );
                  }
                }).toList()
              ),
            );
          }
        ),
      ),
    );
  }
}